//
//  WLMPictureSelectorCell.swift
//  WeiBo
//
//  Created by Eric on 2017-01-16.
//  Copyright © 2017 Eric. All rights reserved.
//

import UIKit

@objc protocol WLMPictureSelectorCellDelegate: NSObjectProtocol {
    @objc optional func userWillAddPic()
    @objc optional func userWillDeletePic(cell: WLMPictureSelectorCell)
}

class WLMPictureSelectorCell: UICollectionViewCell {
    var image: UIImage? {
        didSet {
            addBtn.setImage(image, for: .normal)
            deleteBtn.isHidden = image == nil
            addBtn.imageView?.contentMode = .scaleAspectFill
        }
    }
    
    lazy var addBtn: UIButton = UIButton(backgroundImageName: "compose_pic_add", title: "", fontSize: 0, textColor: UIColor.black)
    lazy var deleteBtn: UIButton = UIButton(backgroundImageName: "compose_photo_close", title: "", fontSize: 0, textColor: UIColor.black)
    
    weak var delegate: WLMPictureSelectorCellDelegate?
    override init(frame: CGRect){
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.addSubview(addBtn)
        contentView.addSubview(deleteBtn)
        //约束
        addBtn.snp.makeConstraints { (make) in
            make.edges.equalTo(self.contentView)
        }
        deleteBtn.snp.makeConstraints { (make) in
            make.top.right.equalTo(self.contentView)
        }
        
        //添加事件
        addBtn.addTarget(self, action: #selector(clickedAddBtn), for: .touchUpInside)
        deleteBtn.addTarget(self, action: #selector(clickDeleteBtn), for: .touchUpInside)
    }
    @objc private func clickedAddBtn() {
        if image != nil {
            return
        }
        delegate?.userWillAddPic?()
    }
    @objc private func clickDeleteBtn() {
        delegate?.userWillDeletePic?(cell: self)
    }
}
